package cc.yihy.web.shiro.filter;

import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.CollectionUtils;
import org.apache.shiro.web.filter.authz.AuthorizationFilter;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.IOException;
import java.util.LinkedList;
import java.util.Set;

/**
 * 
 * @ClassName:NewRolesAuthorizationFilter.java
 * @Description:    
 * @Author: yihy
 * @Since: 2016年9月20日
 * @Version: 1.0
 */
public class AnyRolesAuthorizationFilter extends AuthorizationFilter {


	

    public boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) throws IOException {

        Subject subject = getSubject(request, response);
        String[] rolesArray = (String[]) mappedValue;

        if (rolesArray == null || rolesArray.length == 0) {
            //no roles specified, so nothing to check - allow access.
            return true;
        }

        Set<String> roles = CollectionUtils.asSet(rolesArray);
        boolean[] hasRoles = subject.hasRoles(new LinkedList<>(roles));
        
        for (boolean c : hasRoles) {
        	if(c)
        		return c;
		}
        return false;
    }

    
}

